// ==UserScript==
// @name         邯科校园网自动认证
// @namespace    http://tampermonkey.net/
// @version      2024-04-02
// @description  校园网自动认证
// @author       熠初shine
// @match        http://211.138.1.148:8888/*
// @icon         
// @grant        none
// ==/UserScript==
function createForm()
{
    const box = document.createElement('div');
    box.setAttribute('id','yichu-Box');
    document.body.appendChild(box);


    //用户名输入框
    const usernameBox = document.createElement('div');
    const usernamelabel = document.createElement('span');
    usernamelabel.innerText="用户名"
    const usernameInput = document.createElement('input');
    usernameInput.setAttribute('type','text');
    usernameBox.appendChild(usernamelabel);
    usernameBox.appendChild(usernameInput);
    box.appendChild(usernameBox);

    //密码输入框
    const passwordBox = document.createElement('div');
    const passwordlabel = document.createElement('span');
    passwordlabel.innerText = '密码'
    const passwordInput = document.createElement('input');
    passwordInput.setAttribute('type','password');
    passwordBox.appendChild(passwordlabel);
    passwordBox.appendChild(passwordInput);
    box.appendChild(passwordBox);

    //按钮
    const buttonBox = document.createElement('div');
    const OkButton = document.createElement('button');
    OkButton.innerText="确定";
    const CancelButton = document.createElement('button');
    CancelButton.innerText = '取消';
    buttonBox.appendChild(OkButton);
    buttonBox.appendChild(CancelButton);
    box.appendChild(buttonBox);

    const setting = document.createElement('button')
    setting.innerText = '设置';
    setting.setAttribute('id','yichuButton')
    document.body.appendChild(setting);

    //父盒子样式
    box.style.position = 'fixed'
    box.style.left = '10px'
    box.style.top = '50px'
    box.style.padding = '10px 10px 10px 10px'
    box.style.backgroundColor = "#A6A6A6"
    box.style.overflow = 'hidden'
    box.style.zIndex = '99999'
    box.style.display='none'

    //用户名输入框样式
    usernameInput.style.display = "block"

    //密码输入框样式
    passwordInput.style.display='block'

    setting.style.position='fixed'
    setting.style.left = '10px'
    setting.style.top = '20px'
}

function setlistener()
{
    const OkButton = document.querySelector('#yichu-Box > div:nth-child(3) > button:nth-child(1)')
    const CancelButton = document.querySelector('#yichu-Box > div:nth-child(3) > button:nth-child(2)')
    const settingButton = document.querySelector('#yichuButton')

    settingButton.addEventListener('click',()=>{
        const box = document.querySelector('#yichu-Box');
        box.style.display = 'block'
    })
    CancelButton.addEventListener('click',()=>{
        const box = document.querySelector('#yichu-Box');
        box.style.display = 'none'
    })
    OkButton.addEventListener('click',()=>{
        const usernmaeInput = document.querySelector('#yichu-Box > div:nth-child(1) > input[type=text]')
        const passwordInput = document.querySelector('#yichu-Box > div:nth-child(2) > input[type=password]')
        const pwd = passwordInput.value;
        const uname = usernmaeInput.value;
        localStorage.setItem('USERNAME',uname);
        localStorage.setItem('PASSWORD',pwd);
        location.reload();
    })
}

function getInfo()
{
    const usernmaeInput = document.querySelector('#yichu-Box > div:nth-child(1) > input[type=text]')
    const passwordInput = document.querySelector('#yichu-Box > div:nth-child(2) > input[type=password]')
    const uname = localStorage.getItem('USERNAME');
    const pwd = localStorage.getItem('PASSWORD');
    if(uname && pwd)
    {
        usernmaeInput.value = uname;
        passwordInput.value = pwd;
    } 
    return [uname,pwd];
}

(function() {
    'use strict';
    // Your code here...
    createForm();
    setlistener();
    const username = document.querySelector('#bpssUSERNAME');
    const passworld = document.querySelector('#bpssBUSPWD');
    const loginButton = document.querySelector('#subText > a');

    const info = getInfo();
    if(!info[0] || !info[1])
    {
        alert('未能读取用户信息')
        const settingButton = document.querySelector('#yichuButton')
        settingButton.click();
    }
    else
    {
        username.value = info[0];
        passworld.value = info[1];
        loginButton.click();
    }
})();